home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-03-01 | 13.6 KB | 523 lines | [TEXT/CWIE] |
- //----------------------------------------------------------------------------------------
- // UDebug.cp
- // Copyright 1984-1994 Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __DEBUGGER__
- #include "Debugger.h"
- #endif
-
- #if qDebug || qTheDebugger
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
- #endif
-
- #if qAppleANSI
- #include "UDevConsole.h"
- #endif
- #include "TheDebugger.h"
-
- #include "ClassDesc.h"
- #include "DebugWrite.h"
-
- #include <Processes.h>
-
- //----------------------------------------------------------------------------------------
- // Procedure pointer typedefs
- //----------------------------------------------------------------------------------------
-
- #if qDebug || qTheDebugger
-
- typedef long(*GetProcPtr)(Ptr buffer, long count);
- typedef void(*PutProcPtr)(Ptr buffer, long count);
-
- #endif // qDebug || qTheDebugger
-
- bool gHighLevelDebuggerRunning = false; // True if we were able to launch
- // SourceBug, Sade OR Jasik running
-
- #if qTheDebugger
-
- bool gTheDebuggerIsInstalled = false;
-
- #endif // qDebug || qTheDebugger
-
-
- //----------------------------------------------------------------------------------------
- // Static variable definitions.
- //----------------------------------------------------------------------------------------
-
- #if qDebug || qTheDebugger
-
- static GetProcPtr pGetProc = NULL; // Address of the Proc to Handle
- // Read requests Function
- // DEVGETTEXT(textBuf: Ptr, byteCount:
- // longint): longint;
-
- static PutProcPtr pPutProc = NULL; // Address of the Proc to Handle
- // WriteLn requests Procedure
- // DEVPUTTEXT(textBuf: Ptr, byteCount:
- // longint);
-
- static Boolean pUDebugInitialized = false; // True if InitUDebug has been called
-
- #endif // qDebug || qTheDebugger
-
- #if qDebug
-
- static ProcessSerialNumber pPsn; // The process number of the high level
- // debugger if there is one.
-
- #endif // qDebug
-
-
- //----------------------------------------------------------------------------------------
- // Forward function declarations
- //----------------------------------------------------------------------------------------
-
- #if qDebug || qTheDebugger
- void InstallWriteLnHook();
- #endif
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // InitUDebug: essential initialization (segTable, nonRes left in for compatibility (2.0)
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- #if qDebug || qTheDebugger
-
- void InitializeDebugger()
- {
- if (qTheDebugger && TheDbgr_IsInstalled())
- {
- #if qTheDebugger
- gHighLevelDebuggerRunning = true;
- gTheDebuggerIsInstalled = true;
-
- #if qAppleANSI
- InstallWriteLnHook();
- #endif
-
- SetDebugWriteProc(StdDebugWriteProc);
-
- TheDbgr_Adjust_ClassDescr_Ids(ClassDesc::GetClassList());
- TheDbgr_Init_Ext_Dbgr(); // Initialize Hooks for Jasik
-
- #endif
- }
- #if qDebug && !defined(__PowerPlant__)
- else
- {
- gHighLevelDebuggerRunning = false;
-
- int implement_other_debuggers;
- /*
- if (gConfiguration.hasProcessMgr) {
-
- // First, check to see if the NutraBug Nub exists.
- if ( NubIsInstalled() )
- gHighLevelDebuggerRunning = true;
- else {
- // Check to see if one of the high level debuggers are running (SourceBug or Sade). If
- // so then we will send debug output to it.
-
- OSErr err = FindProcessBySignature('Objr', pPsn, NULL); // Is SourceBug running?
- if (err != noErr)
- err = FindProcessBySignature('sade', pPsn, NULL); // Is Sade running?
- gHighLevelDebuggerRunning = err == noErr ? true : false;
- }
- }
- else
- gHighLevelDebuggerRunning = System6HasDebugger(); // call System 6 debugger existence test
-
- macroDontDeadStrip(TDebugFlagsView);
- InstallWriteLnHook();
- */
-
- }
- #endif
-
- pUDebugInitialized = true;
- } // InitUDebug
-
- #endif // qDebug || qTheDebugger
-
- //----------------------------------------------------------------------------------------
- // DebugTerminate:
- //----------------------------------------------------------------------------------------
-
- #if qDebug || qTheDebugger
-
- void FinalizeDebugger()
- {
- #if qPerform
- // Make sure the performance tools are shut down if they are initialized
- if (pTP2PerfGlobals)
- TerminatePerfMonitor();
- #endif
- } // DebugTerminate
-
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // DebugCanReadLn: Returns true if you can readln to the user
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug && !defined(__PowerPlant__)
-
- bool DebugCanReadLn()
- {
- return false;
- } // DebugCanReadLn
-
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // DebugCanWriteLn: Returns true if you can writeln to the user
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug && !defined(__PowerPlant__)
-
- bool DebugCanWriteLn()
- {
- return pUDebugInitialized;
- } // DebugCanWriteLn
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // EachFrameDo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- #if qDebug && !defined(__PowerPlant__)
- /*
- void EachFrameDo(CStackFrame baseFrame,
- DoToFrameType DoToFrame,
- void* staticLink)
- {
- CStackFrameIterator iter(baseFrame);
- CStackFrame frame;
-
- for (frame = iter.FirstFrame(); iter.More(); frame = iter.NextFrame())
- {
- DoToFrame(frame, staticLink);
- }
- } // EachFrameDo
- */
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // GetCallersMethodName:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug && !defined(__PowerPlant__)
- /*
- void GetCallersMethodName(MAName& s)
- {
- #if qPowerPC
- s = "PowerPC";
- #else
- CStackFrame stackFrame(GetCurStackFramePtr());
- GetMethodName(stackFrame.GetReturnAddress(), s); // report about our caller"s caller
- #endif
- } // GetCallersMethodName
- */
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // GetMethodName: GetMethodName returns the name of the method (or procedure) in which pc
- // points.
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug && !defined(__PowerPlant__)
- /*
- void GetMethodName(void* pc, MAName& s)
- {
- MAName discardStr;
- GetProcName(pc, discardStr, s);
- } // GetMethodName
- */
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // GetProcName: GetProcName returns the name of the procedure or function in which pc
- // points. If it is in a method, then it return"s the name of the method"s class in
- // className.
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug && !defined(__PowerPlant__)
- /*
- void GetProcName(void* pc, ClassName& className, MAName& procName)
- {
- #if !qPowerPC
- if (pc != NULL && !odd((long) pc))
- {
- void* nextPC;
- CStr255 localProcName;
-
- Ptr limit = (Ptr) ((ptrdiff_t) pc + 32767);
- while (!endOfModule(pc, limit, (char *) &localProcName, &nextPC))
- {
- if (pc >= limit)
- {
- className = "";
- procName = "";
- localProcName = "";
- return;
- }
- else
- pc = (void *) ((ptrdiff_t) pc + 2);
- }
-
- char cProcName[256];
-
- if (unmangle(cProcName, (char *) localProcName, 255) < 1)
- procName = localProcName;
- else
- procName = cProcName;
-
- short index = procName.Pos(".");
- if (index != 0)
- className = procName.Copy(1, index - 1);
- else
- className = "";
- }
- else
- #endif
- {
- className = "";
- procName = "";
- }
- } // GetProcName
- */
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // ShowSegments:
- //----------------------------------------------------------------------------------------
-
- #if qDebug && !defined(__PowerPlant__)
-
- void ShowSegments()
- {
- #if qSegments
- // Show segment information. if allSegments is true then also show unloaded && purged
-
- short i;
-
- long codeRes;
- long nSeg;
- Handle seg;
- short id;
- CStr255 name;
- ResType t;
-
-
- codeRes = 0; // counts size of code segments
-
- nSeg = GetHandleSize((Handle) gCodeSegs) / sizeof(Handle);
-
- fprintf(stderr, "Total # segments = %d \n" , nSeg);
- fprintf(stderr, "• = resident, L = loaded\n");
-
- for (i=0; i<nSeg; i++)
- {
-
- seg = (*gCodeSegs)[i];
- if ((seg != 0) && !IsHandlePurged(seg) && IsHandleLocked(seg))
- {
- GetResInfo(seg, &id, &t, name);
-
- WritePtr((long) seg);
-
- fprintf(stderr, " Seg#:%d ", id);
-
- if ((*gIsResidentSeg)[i])
- fprintf(stderr, "• ");
- else if (IsHandlePurged(seg))
- fprintf(stderr, " ");
- else if ((*gIsLoadedSeg)[i])
- fprintf(stderr, "L ");
-
- fprintf(stderr, " %s ", (char *) name);
-
- fprintf(stderr, "%ld bytes\n", (*pSegSize)[i]);
-
- codeRes = codeRes + (*pSegSize)[i] + 8;
- }
- }
-
- fprintf(stderr, "\n");
- fprintf(stderr, "Total loaded code = %ld\n", codeRes);
-
- long lockedSpace = TotalTempSize(true, seg);
- long totalSpace = TotalTempSize(false, seg);
- fprintf(stderr," Current temp space: locked = %ld, unlocked = %ld, total = %ld\n",
- lockedSpace, totalSpace - lockedSpace, totalSpace);
- #endif
- } // ShowSegments
-
- #endif // qDebug
-
- //----------------------------------------------------------------------------------------
- // System6HasDebugger:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if !defined(__MWERKS__)
- #if qDebug || qTheDebugger && !defined(__PowerPlant__)
-
- Boolean System6HasDebugger()
- {
- #if qPowerPC
- return false;
- #else
- const long* kTrap5Vector = (long *)0x94; // 680X0 TRAP #5 Vector
-
- // Check to see if the TRAP #5 vector points between A5 and BufPtr.
- // This would imply that it is pointing into MultiFinder's jumptable,
- // which would indicate that Sade or SourceBug is running
- return (*kTrap5Vector > (long)LMGetCurrentA5() && *kTrap5Vector < (long)LMGetBufPtr());
- #endif
- }
-
- #endif // qDebug || qTheDebugger
- #endif // !defined(__MWERKS__)
-
- //----------------------------------------------------------------------------------------
- // ConfirmHighLevelDebugger:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- #if qDebug || qTheDebugger
-
- #if defined(__MWERKS__) && !defined(__SC__)
- #pragma require_prototypes off
- #endif
-
- bool ConfirmHighLevelDebugger()
- {
- if ( qTheDebugger && (gTheDebuggerIsInstalled || TheDbgr_IsInstalled()))
- gHighLevelDebuggerRunning = true;
-
- #if qDebug && !defined(__PowerPlant__)
- else if (gHighLevelDebuggerRunning)
- {
- /*
- if (gConfiguration.hasProcessMgr)
- {
- if ( NubIsInstalled() )
- gHighLevelDebuggerRunning = true;
- else
- {
- OSErr err;
- ProcessInfoRec processInfo;
-
- // Check to see if the high level debugger is still running. The user could have
- // closed the application. If it is no longer running then just quit sending
- // output to the debugger.
-
- processInfo.processInfoLength = sizeof(ProcessInfoRec);
- processInfo.processName = NULL;
- processInfo.processAppSpec = NULL;
-
- err = GetProcessInformation(&pPsn, &processInfo);
- if (err != noErr)
- gHighLevelDebuggerRunning = false;
- }
- }
- else
- gHighLevelDebuggerRunning = System6HasDebugger();
- */
- return false;
- }
- #endif
-
- return gHighLevelDebuggerRunning;
- } // ConfirmHighLevelDebugger
-
- #endif // qDebug || qTheDebugger
-
- //----------------------------------------------------------------------------------------
- // DebugWriteLnHook:
- //----------------------------------------------------------------------------------------
-
- #if qDebug || qTheDebugger
-
- void DebugWriteLnHook(Ptr textBuf, long byteCount)
- {
- if (ConfirmHighLevelDebugger())
- {
- Str255 debugStr;
-
- Ptr p = textBuf;
-
- while (byteCount > 0)
- {
- long len = byteCount;
-
- if (len > 255)
- {
- len = 255;
- }
-
- debugStr[0] = (unsigned char) len;
-
- BlockMove(p, &debugStr[1], len);
-
- SysBreakFunc(debugStr);
-
- byteCount -= len;
- p += len;
- }
- }
- } // DebugWriteLnHook
-
- #endif // qDebug || qTheDebugger
-
- //----------------------------------------------------------------------------------------
- // InstallWriteLnHook:
- //----------------------------------------------------------------------------------------
-
- #if qAppleANSI
-
- void InstallWriteLnHook()
- {
- #if !(THINK_CPLUS || SYMANTEC_CPLUS)
- #if defined(__MWERKS__) && wantDebugging && defined(__CW5__)
- CWInstallWriteLnHook();
- #else
- #if !defined(__MWERKS__)
- fclose(stdin);
- fclose(stdout);
- fclose(stderr);
- #endif
- const short _CODEV = 1; // console device number
- _addDevHandler(_CODEV, 0, DevFAccess, DevClose, DevRead, DevWrite, DevIoctl);
- #if !defined(__MWERKS__)
- freopen("dev:console", "r", stdin);
- freopen("dev:console", "a", stdout);
- freopen("dev:console", "a", stderr);
- #endif
- SetPutProc(DebugWriteLnHook);
- setvbuf(stderr, NULL, _IOLBF, 128);
- SetGetProc(DebugReadLn);
- #endif
- #endif // !THINK_CPLUS || SYMANTEC_CPLUS
- } // InstallWriteLnHook
-
- #endif // qDebug || qTheDebugger
-
-